//
//  QpFoodDetailsViewController+shouCang.m
//  Tour
//
//  Created by 才诗琦 on 16/3/16.
//  Copyright © 2016年 张中烨. All rights reserved.
//

#import "QpFoodDetailsViewController+shouCang.h"

@implementation QpFoodDetailsViewController (shouCang)
//查询全部
-(NSArray *)selectAllModel{
    //获取查询数据的请求 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"ShouCang"];
    //判断错误
    NSError *error;
    
    self.manager = [CoreDataManager defaults];
                    
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    if (!error) {
        if (array.count != 0) {
            NSLog(@"查询成功:数据个数为%ld", array.count);
        }else{
            NSLog(@"查询成功:数据个数为0");
        }
        NSLog(@"#####%ld",array.count);
        return array;
    }else{
        NSLog(@"数据查询失败");
        return nil;
    }
}

//查询
- (NSArray *)selectHandler:(NSPredicate *)predicate
{
    //获取数据请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"ShouCang"];
    
//    if (predicate != nil) {
        //设置request的谓词
    request.predicate = predicate;
//    }
    
    //数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    //定义error
    NSError *error;
    
    self.manager = [CoreDataManager defaults];
    //获取查询的数组
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    NSLog(@"%ld",array.count);
    
    return array;
}
//删除
- (NSArray *)deleteHandlerWithArray:(NSArray *)table
                              index:(NSInteger)index;
{
    //获取查询数据请求, 相当于数据库查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"ShouCang"];
    //定义error
    NSError *error;
    self.manager = [CoreDataManager defaults];
    NSArray *array  = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    if (array.count != 0) {
        
        ShouCang *stu = [array objectAtIndex:index];
        
        [self.manager.managedObjectContext deleteObject:stu];
        
        [self.manager saveContext];
        
    }
    return array;
}

//增加
- (void)addHandler:(NSEntityDescription *)entity
              name:(NSString *)name
            cityid:(NSString *)cityid
                         photo:(NSString *)photo
{//创建实体
    //参数1: 实体描述
    //参数2: 数据管理器
    self.manager = [CoreDataManager defaults];
    
    ShouCang *stu = [[ShouCang alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    
    
    stu.title = name;

    stu.cityid = [NSNumber numberWithInteger:[cityid integerValue]];
    stu.photo = photo;
// NSMutableArray *array = [NSMutableArray array];
//    [array addObject:name];
//    [array addObject:cityid];
    
    //添加
    [self.manager saveContext];
//    return array;
}

























@end
